package com.ja.openchat.ui.chat.adapter.viewholder;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.PreviewMediaActivity;
import com.ja.openchat.ui.chat.adapter.ConversationMessageAdapter;
import com.ja.openchat.views.FullVideoView;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.file.DocumentInfo;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.message.UiMessage;

import java.util.ArrayList;
import java.util.List;

public abstract class MediaMessageContentViewHolder extends NormalMessageContentViewHolder {

    /**
     * 小视频，图片 占位图的配置
     */
    protected RequestOptions placeholderOptions = new RequestOptions();

    public MediaMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
        placeholderOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
        placeholderOptions.centerCrop();
        placeholderOptions.placeholder(R.drawable.image_chat_placeholder);
    }

    @Override
    protected void onBind(UiMessage message) {

//        if (message.isDownloading) {
//            progressBar.setVisibility(View.VISIBLE);
//        } else {
//            progressBar.setVisibility(View.GONE);
//        }
    }

    private boolean isImageType(MessageInfo message){
        if(message==null){
            return false;
        }
        if(message.messageType == TelegramManager.MessageType.MessageType_Photo){
            return true;
        }
        if(message.messageType == TelegramManager.MessageType.MessageType_Document){
            if(message.content!=null&&message.content.document!=null){
                DocumentInfo documentInfo = message.content.document;
                return DocumentInfo.isImageFile(documentInfo.file_name);
            }
        }
        return false;
    }


    private boolean isMediaType(MessageInfo message){
        if(message==null){
            return false;
        }
        if(message.messageType == TelegramManager.MessageType.MessageType_Photo){
            return true;
        }
        if(message.messageType == TelegramManager.MessageType.MessageType_Video){
            return true;
        }
        if(message.messageType == TelegramManager.MessageType.MessageType_Animation){
            return true;
        }
        if(message.messageType == TelegramManager.MessageType.MessageType_Document){
            if(message.content!=null&&message.content.document!=null){
                DocumentInfo documentInfo = message.content.document;
                if(DocumentInfo.isImageFile(documentInfo.file_name)||DocumentInfo.isVideoFile(documentInfo.file_name)){
                    return true;
                }
            }
        }
        return false;
    }

    protected void previewMM() {

        List<UiMessage> messages = ((ConversationMessageAdapter) adapter).getMessages();
        UiMessage msg;
        List<MessageInfo> entries = new ArrayList<>();
        int current = 0;
        int index = 0;
        for (int i = 0; i < messages.size(); i++) {
            msg = messages.get(i);
            if (!isMediaType(msg.message)) {
                continue;
            }
            entries.add(msg.message);
            if (message.message.id == msg.message.id) {
                current = index;
            }
            index++;
        }
        if (entries.isEmpty()) {
            return;
        }
        PreviewMediaActivity.previewMedia(fragment.getContext(), entries,current);
//        if(isImageType(message.message)){
//
//        }else{
//            PreviewMediaActivity.previewVideo(fragment.getContext(), message.message);
//        }

    }

    /**
     * 图片 和小视频 加载的地方
     * 策略是先加载缩略图，在加载原图
     * dhl
     * @param thumbnail
     * @param imagePath
     * @param imageView
     */
    protected void loadMedia(String thumbnail, String imagePath, ImageView imageView){
        RequestBuilder<Drawable> thumbnailRequest = null;
        if(thumbnail != null) {
            thumbnailRequest = Glide
                    .with(fragment)
                    .load(thumbnail);
        }else{
            thumbnailRequest = Glide
                    .with(fragment)
                    .load(R.drawable.image_chat_placeholder);
        }
        Glide.with(fragment)
                .load(imagePath)
                .thumbnail(thumbnailRequest)
                .apply(placeholderOptions)
                .into(imageView);

    }

    protected void loadMediaAsGif(boolean isDownloadViedeo, String imagePath, FullVideoView mVideoView, ImageView imageView,View playImageView){
        if(mVideoView.getTag()!=null&&((int)mVideoView.getTag())==110){
            return;
        }
        Glide.with(fragment)
                .load(imagePath)
                .placeholder(R.mipmap.gif_default_img)
                .into(imageView);
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer iMediaPlayer, int what, int i1) {
                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                            mVideoView.setBackgroundColor(Color.TRANSPARENT);
                        return true;
                    }
                });
                mp.setLooping(true);
//                if(SettingManager.getInstance().isAutoPlayGif()||message.isPlaying){
                    playImageView.setVisibility(View.GONE);
                    mp.start();
//                }else if(!message.isPlaying){
//                    imageView.setVisibility(View.VISIBLE);
//                    playImageView.setVisibility(View.VISIBLE);
//                }
            }
        });
        mVideoView.setOnErrorListener((mp, what, extra) -> {
            imageView.setVisibility(View.VISIBLE);
            playImageView.setVisibility(View.VISIBLE);
            return true;
        });
        if(isDownloadViedeo
//                &&(SettingManager.getInstance().isAutoPlayGif()||message.isPlaying)
        ){

//            mVideoView.setTag(110);
            if(!DocumentInfo.isGifeFile(imagePath)){
                imageView.setVisibility(View.INVISIBLE);
                mVideoView.setVisibility(View.VISIBLE);
                mVideoView.setVideoPath(imagePath);
                mVideoView.start();
            }else{
                imageView.setVisibility(View.VISIBLE);
                mVideoView.setVisibility(View.INVISIBLE);
            }
            playImageView.setVisibility(View.GONE);
        }else{
            playImageView.setVisibility(View.VISIBLE);
            mVideoView.setTag(111);
            mVideoView.setVisibility(View.INVISIBLE);
            imageView.setVisibility(View.VISIBLE);
        }

    }
}
